package com.api.smoke.wall;

import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.CollectionResultApi;
import com.zyncro.api.model.EventApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetEventsSmoke extends AbstractApiTest {

	private CollectionResultApi<EventApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		String fileName = getRepo().getPropertyDto(domainName + "-Events", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");
				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {
					CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getEvents(null, 0, null, false, true,
							true, null, null);

					DataUtil.saveDtoToFile(result, fileName);
				} catch (Exception e) {

					throw new IllegalStateException("Exception creating test data", e);
				}
				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<CollectionResultApi<EventApi>>() {
		});
	}

	@Test
	public void testGetAllEvents() throws ZyncroApiException {

		CollectionResultApi<EventApi> result = getApi().getService(IWallApiService.class).getEvents(null, 0, null, false, true, true, null,
				null);

		AssertUtil.assertDTOEquals(result.getElements(), expectedResults.getElements());
	}
}
